using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Product;
using System.IO;

public partial class admin_webparts_Plug_ins_Product_ProductTypeInput : System.Web.UI.UserControl
{
    public bool _IsAdmin = false;
    public ProductGroup _Group = null;
    public ProductType _ParentType = null;
    public ProductType _Type = null;
    public ProductTypeConfig _ParentConfig = null;
    public ProductTypeConfig _Config = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this._IsAdmin = "admin".Equals(Context.User.Identity.Name, StringComparison.OrdinalIgnoreCase);

        if (!this._IsAdmin && "0".Equals(Request.QueryString["typeid"], StringComparison.OrdinalIgnoreCase))
        {
            Response.Redirect("ProductContent.aspx?action=err&err=notauthorised", false);
            return;
        }
        /*
        if (this._IsAdmin)
        {
            tbConfig.Visible = tipConfig.Visible = true;
        }
        else
        {
            tbConfig.Visible = tipConfig.Visible = false;
        }
        */
        if (!IsPostBack)
        {
            int langID = LanguageUtility.GetLanguageID(Request, Session);
            int siteID = WebGlobal.GetSite().ID;

            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("PRODUCT", Session), "0"));
            this.cmbContentType.Items.Add(new ListItem(LanguageUtility.GetLanguageText("VIEWTYPE_LINK", Session), "1"));

            txtImage1.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 1";
            txtImage2.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 2";
            txtImage3.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 3";
            txtImage4.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 4";
            txtImage5.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 5";
            txtImage6.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 6";
            txtImage7.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 7";
            txtImage8.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 8";
            txtImage9.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 9";
            txtImage10.Text = LanguageUtility.GetLanguageText("IMAGE", Session) + " 10";
            try
            {
                int groupID = Int32.Parse(Request.QueryString["g"].Trim());
                _Group = new ProductGroup(groupID);

                DataTable tbType = ProductPropertyType.GetAll(siteID, langID);
                if (tbType != null)
                {
                    cmbProp1Type.DataSource
                        = cmbProp2Type.DataSource
                        = cmbProp3Type.DataSource
                        = cmbProp4Type.DataSource
                        = cmbProp5Type.DataSource
                        = cmbProp6Type.DataSource
                        = cmbProp7Type.DataSource
                        = cmbProp8Type.DataSource
                        = cmbProp9Type.DataSource
                        = cmbProp10Type.DataSource
                        = tbType;
                    cmbProp1Type.DataTextField
                        = cmbProp2Type.DataTextField
                        = cmbProp3Type.DataTextField
                        = cmbProp4Type.DataTextField
                        = cmbProp5Type.DataTextField
                        = cmbProp6Type.DataTextField
                        = cmbProp7Type.DataTextField
                        = cmbProp8Type.DataTextField
                        = cmbProp9Type.DataTextField
                        = cmbProp10Type.DataTextField
                        = ProductPropertyType.NAME__COLUMN_NAME;
                    cmbProp1Type.DataValueField
                        = cmbProp2Type.DataValueField
                        = cmbProp3Type.DataValueField
                        = cmbProp4Type.DataValueField
                        = cmbProp5Type.DataValueField
                        = cmbProp6Type.DataValueField
                        = cmbProp7Type.DataValueField
                        = cmbProp8Type.DataValueField
                        = cmbProp9Type.DataValueField
                        = cmbProp10Type.DataValueField
                        = ProductPropertyType.ID__COLUMN_NAME;
                    cmbProp1Type.DataBind();
                    cmbProp2Type.DataBind();
                    cmbProp3Type.DataBind();
                    cmbProp4Type.DataBind();
                    cmbProp5Type.DataBind();
                    cmbProp6Type.DataBind();
                    cmbProp7Type.DataBind();
                    cmbProp8Type.DataBind();
                    cmbProp9Type.DataBind();
                    cmbProp10Type.DataBind();
                }

                DataTable tbTypeColl = ProductPropertyCollection.GetAll(siteID, langID, 1);
                if (tbTypeColl != null)
                {
                    cmbProp1TypeColl.DataSource
                        = cmbProp2TypeColl.DataSource
                        = cmbProp3TypeColl.DataSource
                        = cmbProp4TypeColl.DataSource
                        = cmbProp5TypeColl.DataSource
                        = cmbProp6TypeColl.DataSource
                        = cmbProp7TypeColl.DataSource
                        = cmbProp8TypeColl.DataSource
                        = cmbProp9TypeColl.DataSource
                        = cmbProp10TypeColl.DataSource 
                        = tbTypeColl;
                    cmbProp1TypeColl.DataTextField
                        = cmbProp2TypeColl.DataTextField
                        = cmbProp3TypeColl.DataTextField
                        = cmbProp4TypeColl.DataTextField
                        = cmbProp5TypeColl.DataTextField
                        = cmbProp6TypeColl.DataTextField
                        = cmbProp7TypeColl.DataTextField
                        = cmbProp8TypeColl.DataTextField
                        = cmbProp9TypeColl.DataTextField
                        = cmbProp10TypeColl.DataTextField
                        = ProductPropertyCollection.NAME__COLUMN_NAME;
                    cmbProp1TypeColl.DataValueField
                        = cmbProp2TypeColl.DataValueField
                        = cmbProp3TypeColl.DataValueField
                        = cmbProp4TypeColl.DataValueField
                        = cmbProp5TypeColl.DataValueField
                        = cmbProp6TypeColl.DataValueField
                        = cmbProp7TypeColl.DataValueField
                        = cmbProp8TypeColl.DataValueField
                        = cmbProp9TypeColl.DataValueField
                        = cmbProp10TypeColl.DataValueField 
                        = ProductPropertyCollection.ID__COLUMN_NAME;
                    cmbProp1TypeColl.DataBind();
                    cmbProp2TypeColl.DataBind();
                    cmbProp3TypeColl.DataBind();
                    cmbProp4TypeColl.DataBind();
                    cmbProp5TypeColl.DataBind();
                    cmbProp6TypeColl.DataBind();
                    cmbProp7TypeColl.DataBind();
                    cmbProp8TypeColl.DataBind();
                    cmbProp9TypeColl.DataBind();
                    cmbProp10TypeColl.DataBind();
                }

                DataTable tbMultiValueType = ProductPropertyMultiValueType.GetAll(siteID, langID);
                if (tbMultiValueType != null)
                {
                    cmbProp1TypeMulti.DataSource
                        = cmbProp2TypeMulti.DataSource
                        = cmbProp3TypeMulti.DataSource
                        = cmbProp4TypeMulti.DataSource
                        = cmbProp5TypeMulti.DataSource
                        = cmbProp6TypeMulti.DataSource
                        = cmbProp7TypeMulti.DataSource
                        = cmbProp8TypeMulti.DataSource
                        = cmbProp9TypeMulti.DataSource
                        = cmbProp10TypeMulti.DataSource
                        = tbMultiValueType;
                    cmbProp1TypeMulti.DataTextField
                        = cmbProp2TypeMulti.DataTextField
                        = cmbProp3TypeMulti.DataTextField
                        = cmbProp4TypeMulti.DataTextField
                        = cmbProp5TypeMulti.DataTextField
                        = cmbProp6TypeMulti.DataTextField
                        = cmbProp7TypeMulti.DataTextField
                        = cmbProp8TypeMulti.DataTextField
                        = cmbProp9TypeMulti.DataTextField
                        = cmbProp10TypeMulti.DataTextField
                        = ProductPropertyMultiValueType.NAME__COLUMN_NAME;
                    cmbProp1TypeMulti.DataValueField
                        = cmbProp2TypeMulti.DataValueField
                        = cmbProp3TypeMulti.DataValueField
                        = cmbProp4TypeMulti.DataValueField
                        = cmbProp5TypeMulti.DataValueField
                        = cmbProp6TypeMulti.DataValueField
                        = cmbProp7TypeMulti.DataValueField
                        = cmbProp8TypeMulti.DataValueField
                        = cmbProp9TypeMulti.DataValueField
                        = cmbProp10TypeMulti.DataValueField
                        = ProductPropertyMultiValueType.ID__COLUMN_NAME;
                    cmbProp1TypeMulti.DataBind();
                    cmbProp2TypeMulti.DataBind();
                    cmbProp3TypeMulti.DataBind();
                    cmbProp4TypeMulti.DataBind();
                    cmbProp5TypeMulti.DataBind();
                    cmbProp6TypeMulti.DataBind();
                    cmbProp7TypeMulti.DataBind();
                    cmbProp8TypeMulti.DataBind();
                    cmbProp9TypeMulti.DataBind();
                    cmbProp10TypeMulti.DataBind();
                }

                int typeID = Int32.Parse(Request.QueryString["typeid"].Trim());
                string action = Request.QueryString["action"].Trim();

                switch (action)
                {
                    case "addtype":
                        if (typeID > 0)
                        {
                            _ParentType = new ProductType(typeID);

                            if (_ParentType.ID < 1)
                            {
                                throw new Exception();
                            }
                        }
                        break;
                    case "edittype":
                        if (typeID > 0)
                        {
                            _Type = new ProductType(typeID);
                            if (_Type.ID > 0)
                            {
                                if (_Type.ParentID > 0)
                                {
                                    _ParentType = new ProductType(_Type.ParentID);
                                }
                            }
                            else
                            {
                                throw new Exception();
                            }
                        }
                        else
                        {
                            throw new Exception();
                        }
                        break;
                    default:
                        throw new Exception();
                }

                if (_Type != null)
                {
                    _Config = ProductTypeConfig.GetConfigByType(_Type.GroupID, _Type.ID);
                }
                if (_ParentType != null)
                {
                    _ParentConfig = ProductTypeConfig.GetConfigByType(_ParentType.GroupID, _ParentType.ID);
                }
                else
                {
                    _ParentConfig = ProductTypeConfig.GetConfigByType(_Group.ID, 0);
                }

                if (_Type != null)
                {
                    this.txtName.Text = _Type.Name;
                    this.txtDescription.Text = _Type.Description;
                    this.chkActive.Checked = _Type.Actived;

                    this.cmbContentType.SelectedValue = _Type.LinkType.ToString();
                    this.txtContentURL.Text = _Type.LinkURL;
                    this.cmbContentTarget.SelectedValue = _Type.LinkTarget;

                    this.chkUseURL.Checked = _Type.ViewType == 1;
                    this.txtURL.Text = _Type.ViewerURL;
                }

                ProductTypeConfig config = _Config == null ? _ParentConfig : _Config;

                if (config != null)
                {
                    chkUnitVisible.Checked = config.UnitVisible;
                    chkSaleOffVisible.Checked = config.SaleOffVisible;
                    chkSoldOutVisible.Checked = config.SoldOutVisible;

                    chkAutoResizePreImage.Checked = config.ResizePreImage;
                    txtMaxPreImageWidth.Text = config.PreImageWidth.ToString();
                    txtMaxPreImageHeight.Text = config.PreImageHeight.ToString();
                    cmbPreImageResizeOption.Value = config.PreImageResizeOption.ToString();
                    txtPreImageBackColor.Text = config.PreImageBackColor;

                    chkAutoResize.Checked = config.AutoResizeImage;
                    txtMaxImageWidth.Text = config.MaxImageWidth.ToString();
                    txtMaxImageHeight.Text = config.MaxImageHeight.ToString();
                    cmbImageResizeOption.Value = config.ImageResizeOption.ToString();
                    txtImageBackColor.Text = config.ImageBackColor;

                    chkCreateMiniImage.Checked = config.AutoMiniImage;
                    txtMiniImageWidth.Text = config.MiniImageWidth.ToString();
                    txtMiniImageHeight.Text = config.MiniImageHeight.ToString();
                    cmbMiniImageResizeOption.Value = config.MiniImageResizeOption.ToString();
                    txtMiniImageBackColor.Text = config.MiniImageBackColor;

                    cmbNumberOfImage.Value = config.NumberOfImage.ToString();
                    txtPresentativeImage.Text = config.Image;
                    txtImage1.Text = config.Image_1;
                    txtImage2.Text = config.Image_2;
                    txtImage3.Text = config.Image_3;
                    txtImage4.Text = config.Image_4;
                    txtImage5.Text = config.Image_5;
                    txtImage6.Text = config.Image_6;
                    txtImage7.Text = config.Image_7;
                    txtImage8.Text = config.Image_8;
                    txtImage9.Text = config.Image_9;
                    txtImage10.Text = config.Image_10;

                    cmbNumberOfProperty.Value = config.NumberOfProperty.ToString();
                    txtProperty1.Text = config.Property_1;
                    txtProperty2.Text = config.Property_2;
                    txtProperty3.Text = config.Property_3;
                    txtProperty4.Text = config.Property_4;
                    txtProperty5.Text = config.Property_5;
                    txtProperty6.Text = config.Property_6;
                    txtProperty7.Text = config.Property_7;
                    txtProperty8.Text = config.Property_8;
                    txtProperty9.Text = config.Property_9;
                    txtProperty10.Text = config.Property_10;

                    cmbProp1Type.Value = config.Prop1Type.ToString();
                    cmbProp2Type.Value = config.Prop2Type.ToString();
                    cmbProp3Type.Value = config.Prop3Type.ToString();
                    cmbProp4Type.Value = config.Prop4Type.ToString();
                    cmbProp5Type.Value = config.Prop5Type.ToString();
                    cmbProp6Type.Value = config.Prop6Type.ToString();
                    cmbProp7Type.Value = config.Prop7Type.ToString();
                    cmbProp8Type.Value = config.Prop8Type.ToString();
                    cmbProp9Type.Value = config.Prop9Type.ToString();
                    cmbProp10Type.Value = config.Prop10Type.ToString();

                    if (config.Prop1Type == 2)
                    {
                        cmbProp1TypeColl.Value = config.Prop1CollID.ToString();
                    }
                    else if (config.Prop1Type == 3)
                    {
                        cmbProp1TypeMulti.Value = config.Prop1CollID.ToString();
                    }
                    if (config.Prop2Type == 2)
                    {
                        cmbProp2TypeColl.Value = config.Prop2CollID.ToString();
                    }
                    else if (config.Prop2Type == 3)
                    {
                        cmbProp2TypeMulti.Value = config.Prop2CollID.ToString();
                    }
                    if (config.Prop3Type == 2)
                    {
                        cmbProp3TypeColl.Value = config.Prop3CollID.ToString();
                    }
                    else if (config.Prop3Type == 3)
                    {
                        cmbProp3TypeMulti.Value = config.Prop3CollID.ToString();
                    }
                    if (config.Prop4Type == 2)
                    {
                        cmbProp4TypeColl.Value = config.Prop4CollID.ToString();
                    }
                    else if (config.Prop4Type == 3)
                    {
                        cmbProp4TypeMulti.Value = config.Prop4CollID.ToString();
                    }
                    if (config.Prop5Type == 2)
                    {
                        cmbProp5TypeColl.Value = config.Prop5CollID.ToString();
                    }
                    else if (config.Prop5Type == 3)
                    {
                        cmbProp5TypeMulti.Value = config.Prop5CollID.ToString();
                    }
                    if (config.Prop6Type == 2)
                    {
                        cmbProp6TypeColl.Value = config.Prop6CollID.ToString();
                    }
                    else if (config.Prop6Type == 3)
                    {
                        cmbProp6TypeMulti.Value = config.Prop6CollID.ToString();
                    }
                    if (config.Prop7Type == 2)
                    {
                        cmbProp7TypeColl.Value = config.Prop7CollID.ToString();
                    }
                    else if (config.Prop7Type == 3)
                    {
                        cmbProp7TypeMulti.Value = config.Prop7CollID.ToString();
                    }
                    if (config.Prop8Type == 2)
                    {
                        cmbProp8TypeColl.Value = config.Prop8CollID.ToString();
                    }
                    else if (config.Prop8Type == 3)
                    {
                        cmbProp8TypeMulti.Value = config.Prop8CollID.ToString();
                    }
                    if (config.Prop9Type == 2)
                    {
                        cmbProp9TypeColl.Value = config.Prop9CollID.ToString();
                    }
                    else if (config.Prop9Type == 3)
                    {
                        cmbProp9TypeMulti.Value = config.Prop9CollID.ToString();
                    }
                    if (config.Prop10Type == 2)
                    {
                        cmbProp10TypeColl.Value = config.Prop10CollID.ToString();
                    }
                    else if (config.Prop10Type == 3)
                    {
                        cmbProp10TypeMulti.Value = config.Prop10CollID.ToString();
                    }

                    txtProp1DefaultValue.Text = config.Prop1DefaultValue;
                    txtProp2DefaultValue.Text = config.Prop2DefaultValue;
                    txtProp3DefaultValue.Text = config.Prop3DefaultValue;
                    txtProp4DefaultValue.Text = config.Prop4DefaultValue;
                    txtProp5DefaultValue.Text = config.Prop5DefaultValue;
                    txtProp6DefaultValue.Text = config.Prop6DefaultValue;
                    txtProp7DefaultValue.Text = config.Prop7DefaultValue;
                    txtProp8DefaultValue.Text = config.Prop8DefaultValue;
                    txtProp9DefaultValue.Text = config.Prop9DefaultValue;
                    txtProp10DefaultValue.Text = config.Prop10DefaultValue;
                }
            }
            catch (Exception)
            {
                Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ProductType type = null;
            ProductTypeConfig config = null;
            string action = Request.QueryString["action"].Trim();
            int groupID = Int32.Parse(Request.QueryString["g"].Trim());
            int typeID = Int32.Parse(Request.QueryString["typeid"].Trim());

            switch (action)
            {
                case "addtype":
                    type = new ProductType();
                    type.ParentID = typeID;
                    type.GroupID = groupID;
                    config = new ProductTypeConfig();
                    config.GroupID = groupID;
                    break;
                case "edittype":
                    type = new ProductType(typeID);
                    config = ProductTypeConfig.GetConfigByType(type.GroupID, typeID);
                    if (config == null)
                    {
                        config = new ProductTypeConfig();
                    }
                    break;
                default:
                    throw new Exception();
            }

            type.Name = txtName.Text;
            type.Actived = chkActive.Checked;
            type.Description = txtDescription.Text;

            type.ViewType = this.chkUseURL.Checked ? 1 : 0;
            type.ViewerURL = this.txtURL.Text.Trim();

            type.LinkType = Convert.ToInt32(this.cmbContentType.SelectedValue);
            type.LinkURL = this.txtContentURL.Text;
            type.LinkTarget = this.cmbContentTarget.SelectedValue;
            //type.ViewType = Convert.ToInt32(txtViewType.Text.Trim());

            config.UnitVisible = chkUnitVisible.Checked;
            config.SaleOffVisible = chkSaleOffVisible.Checked;
            config.SoldOutVisible = chkSoldOutVisible.Checked;

            config.ResizePreImage = chkAutoResizePreImage.Checked;
            config.PreImageWidth = Convert.ToInt32(txtMaxPreImageWidth.Text.Trim());
            config.PreImageHeight = Convert.ToInt32(txtMaxPreImageHeight.Text.Trim());
            config.PreImageResizeOption = Convert.ToInt16(cmbPreImageResizeOption.Value);
            config.PreImageBackColor = txtPreImageBackColor.Text.Trim().Replace("#", "").Substring(0, 6);

            config.AutoResizeImage = chkAutoResize.Checked;
            config.MaxImageWidth = Convert.ToInt32(txtMaxImageWidth.Text.Trim());
            config.MaxImageHeight = Convert.ToInt32(txtMaxImageHeight.Text.Trim());
            config.ImageResizeOption = Convert.ToInt16(cmbImageResizeOption.Value);
            config.ImageBackColor = txtImageBackColor.Text.Trim().Replace("#", "").Substring(0, 6);

            config.AutoMiniImage = chkCreateMiniImage.Checked;
            config.MiniImageWidth = Convert.ToInt32(txtMiniImageWidth.Text.Trim());
            config.MiniImageHeight = Convert.ToInt32(txtMiniImageHeight.Text.Trim());
            config.MiniImageResizeOption = Convert.ToInt16(cmbMiniImageResizeOption.Value);
            config.MiniImageBackColor = txtMiniImageBackColor.Text.Trim().Replace("#", "").Substring(0, 6);

            config.Image = txtPresentativeImage.Text;
            config.NumberOfImage = Int32.Parse(cmbNumberOfImage.Value);
            config.Image_1 = txtImage1.Text;
            config.Image_2 = txtImage2.Text;
            config.Image_3 = txtImage3.Text;
            config.Image_4 = txtImage4.Text;
            config.Image_5 = txtImage5.Text;
            config.Image_6 = txtImage6.Text;
            config.Image_7 = txtImage7.Text;
            config.Image_8 = txtImage8.Text;
            config.Image_9 = txtImage9.Text;
            config.Image_10 = txtImage10.Text;

            config.NumberOfProperty = Int32.Parse(cmbNumberOfProperty.Value);
            config.Property_1 = txtProperty1.Text;
            config.Property_2 = txtProperty2.Text;
            config.Property_3 = txtProperty3.Text;
            config.Property_4 = txtProperty4.Text;
            config.Property_5 = txtProperty5.Text;
            config.Property_6 = txtProperty6.Text;
            config.Property_7 = txtProperty7.Text;
            config.Property_8 = txtProperty8.Text;
            config.Property_9 = txtProperty9.Text;
            config.Property_10 = txtProperty10.Text;

            config.Prop1Type = Convert.ToByte(cmbProp1Type.Value);
            config.Prop2Type = Convert.ToByte(cmbProp2Type.Value);
            config.Prop3Type = Convert.ToByte(cmbProp3Type.Value);
            config.Prop4Type = Convert.ToByte(cmbProp4Type.Value);
            config.Prop5Type = Convert.ToByte(cmbProp5Type.Value);
            config.Prop6Type = Convert.ToByte(cmbProp6Type.Value);
            config.Prop7Type = Convert.ToByte(cmbProp7Type.Value);
            config.Prop8Type = Convert.ToByte(cmbProp8Type.Value);
            config.Prop9Type = Convert.ToByte(cmbProp9Type.Value);
            config.Prop10Type = Convert.ToByte(cmbProp10Type.Value);

            config.Prop1CollID = config.Prop1Type == 3 ? Convert.ToInt32(cmbProp1TypeMulti.Value) : Convert.ToInt32(cmbProp1TypeColl.Value);
            config.Prop2CollID = config.Prop2Type == 3 ? Convert.ToInt32(cmbProp2TypeMulti.Value) : Convert.ToInt32(cmbProp2TypeColl.Value);
            config.Prop3CollID = config.Prop3Type == 3 ? Convert.ToInt32(cmbProp3TypeMulti.Value) : Convert.ToInt32(cmbProp3TypeColl.Value);
            config.Prop4CollID = config.Prop4Type == 3 ? Convert.ToInt32(cmbProp4TypeMulti.Value) : Convert.ToInt32(cmbProp4TypeColl.Value);
            config.Prop5CollID = config.Prop5Type == 3 ? Convert.ToInt32(cmbProp5TypeMulti.Value) : Convert.ToInt32(cmbProp5TypeColl.Value);
            config.Prop6CollID = config.Prop6Type == 3 ? Convert.ToInt32(cmbProp6TypeMulti.Value) : Convert.ToInt32(cmbProp6TypeColl.Value);
            config.Prop7CollID = config.Prop7Type == 3 ? Convert.ToInt32(cmbProp7TypeMulti.Value) : Convert.ToInt32(cmbProp7TypeColl.Value);
            config.Prop8CollID = config.Prop8Type == 3 ? Convert.ToInt32(cmbProp8TypeMulti.Value) : Convert.ToInt32(cmbProp8TypeColl.Value);
            config.Prop9CollID = config.Prop9Type == 3 ? Convert.ToInt32(cmbProp9TypeMulti.Value) : Convert.ToInt32(cmbProp9TypeColl.Value);
            config.Prop10CollID = config.Prop10Type == 3 ? Convert.ToInt32(cmbProp10TypeMulti.Value) : Convert.ToInt32(cmbProp10TypeColl.Value);

            config.Prop1DefaultValue = txtProp1DefaultValue.Text;
            config.Prop2DefaultValue = txtProp2DefaultValue.Text;
            config.Prop3DefaultValue = txtProp3DefaultValue.Text;
            config.Prop4DefaultValue = txtProp4DefaultValue.Text;
            config.Prop5DefaultValue = txtProp5DefaultValue.Text;
            config.Prop6DefaultValue = txtProp6DefaultValue.Text;
            config.Prop7DefaultValue = txtProp7DefaultValue.Text;
            config.Prop8DefaultValue = txtProp8DefaultValue.Text;
            config.Prop9DefaultValue = txtProp9DefaultValue.Text;
            config.Prop10DefaultValue = txtProp10DefaultValue.Text;

            string uploadedFileName = String.Empty;
            //string filePath = "../resources/uploaded/product/type/";
            string filePath = "../" + Constant.UPLOAD_FILE_PATH + "product/type/";
            if (this.fuPresentativeImage.HasFile)
            {
                if (this.fuPresentativeImage.PostedFile.FileName.Contains("."))
                {
                    uploadedFileName = DateTime.Now.Ticks.ToString() + Path.GetExtension(this.fuPresentativeImage.PostedFile.FileName);
                        //+ this.fuPresentativeImage.PostedFile.FileName.Substring(this.fuPresentativeImage.PostedFile.FileName.LastIndexOf("."));
                    this.fuPresentativeImage.PostedFile.SaveAs(Server.MapPath(filePath + uploadedFileName));
                }
            }

            if ("edittype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                if (uploadedFileName != string.Empty)
                {
                    if (!System.IO.Directory.Exists(Server.MapPath(filePath + type.ID.ToString() + "/")))
                    {
                        System.IO.Directory.CreateDirectory(Server.MapPath(filePath + type.ID.ToString() + "/"));
                    }

                    if (System.IO.File.Exists(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage));
                    }

                    type.RepresentativeImage = "Type_" + type.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(uploadedFileName);
                    if (System.IO.File.Exists(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage));
                    }
                    System.IO.File.Move(Server.MapPath(filePath + uploadedFileName), Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage));
                }
                type.Update();

                if (config.ID > 0)
                {
                    config.Update();
                }
                else
                {
                    config.TypeID = type.ID;
                    config.Insert();
                }

                Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
            }
            else if ("addtype".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                type.RepresentativeImage = "";
                type.Insert();

                if (!System.IO.Directory.Exists(Server.MapPath(filePath + type.ID.ToString() + "/")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(filePath + type.ID.ToString() + "/"));
                }
                if (uploadedFileName != string.Empty)
                {
                    type.RepresentativeImage = "Type_" + type.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(uploadedFileName);
                    if (System.IO.File.Exists(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage)))
                    {
                        System.IO.File.Delete(Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage));
                    }
                    System.IO.File.Move(Server.MapPath(filePath + uploadedFileName), Server.MapPath(filePath + type.ID.ToString() + "/" + type.RepresentativeImage));
                    type.Update();
                }

                config.TypeID = type.ID;
                config.Insert();

                Response.Redirect("ProductContent.aspx?action=reloadtop&typeid=" + type.ID.ToString("###"), false);
            }
            else
            {
                throw new Exception();
            }
        }
        catch (Exception)
        {
            Response.Redirect("ProductContent.aspx?action=view&g=" + Request.QueryString["g"] + "&typeid=" + Request.QueryString["typeid"] + "&time=" + DateTime.Now.Ticks.ToString(), false);
        }
    }
}
